package cn.anquing.rabbitmq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
public class MQReceiver {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @RabbitListener(queues = MQConfig.QUEUE)
    public void receiver1(String message){
        logger.info("receiver1 message:{}",message);
    }

    @RabbitListener(queues = MQConfig.QUEUE)
    public void receiver2(String message){
        logger.info("receiver2 message:{}",message);
        throw new RuntimeException();
    }

    @RabbitListener(queues = MQConfig.TOPIC_QUEUE1)
    public void receiverTopic1(String message){
        logger.info("receiver topic1 message:{}",message);
    }

    @RabbitListener(queues = MQConfig.TOPIC_QUEUE2)
    public void receiverTopic2(String message){
        logger.info("receiver topic2 message:{}",message);
    }

    /**
     * 通过注解的方式进行绑定
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MQConfig.TOPIC_QUEUE2,
                    durable = "true"),
            exchange = @Exchange(value = MQConfig.TOPIC_EXCHANGE,
                    type = "topic",
                    ignoreDeclarationExceptions = "true"),
            key = "topic.*"
    ))
    public void receiverTopic3(String message){
        logger.info("receiver topic3 message:{}",message);
    }

    @RabbitListener(queues = MQConfig.HEADER_QUEUE)
    public void receiverHeader(byte[] message){
        logger.info("receiver header message:{}",new String(message));
    }
}
